package com.focusai.efairy.model.request;

import android.text.TextUtils;

import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.response.ProjectListResponse;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.ParseException;
import com.focusai.efairy.network.request.base.Request;
import com.focusai.efairy.network.utils.UrlUtils;
import com.focusai.efairy.utils.GsonHelper;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.util.Map;

/**
 * Created by luo_guangwen on 2017/8/12.
 */

public class GetManageProjectListRequest extends Request<ProjectListResponse> {
    private String keyword;
    private int manage_type;//0-自己创建的项目，1-他人创建自己为管理员的项目，默认是0，V1.1新增参数

    public GetManageProjectListRequest(String keyword, int manage_type, Response.Listener<ProjectListResponse> listener) {
        super(Method.GET, UrlUtils.createUrl("/appapi/manage_project_list"), listener);
        this.keyword = keyword;
        this.manage_type = manage_type;
    }

    @Override
    public Map<String, String> getParams() throws JSONException {
        addParam("efairyuser_id", CacheManager.getUserId());
        addParam("access_token", CacheManager.getAccessToken());
        addParam("manage_type", String.valueOf(manage_type));
        if (!TextUtils.isEmpty(keyword)) {
            addParam("keyword", keyword);
        }
        return super.getParams();
    }

    @Override
    protected ProjectListResponse parse(String result) throws ParseException {
        return GsonHelper.getInstance().fromJson(result, new TypeToken<ProjectListResponse>() {
        }.getType());
    }

}
